package com.villaday.idf.activity;

import android.content.Intent;
import android.os.Handler;

import androidx.viewpager.widget.ViewPager;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.AppContext;
import com.villaday.idf.MainActivity;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.pop.AgreementPop;
import com.villaday.idf.model.GuidesEntity;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.StatusBarUtil;
import com.villaday.idf.util.StatusBarUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 启动页0
 */
public class Logo0Activity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener, OkHttpUtil.OnDataListener {

    @BindView(R.id.tv_skip)
    public TextView tvSkip;
    @BindView(R.id.img)
    ImageView img;

   /* @BindView(R.id.sdv)
    SimpleDraweeView sdv;*/


    private boolean isFirst;//是否首次启动
    private int countTime;
    private Handler handler;
    private List<ImageView> imgData;
    private Gson gson = new GsonBuilder().create();
    private boolean isHaveImg = false;//是否展示广告页


    private Runnable runnable;
    private int position = -1;
    private GuidesEntity guidesEntity;
    private boolean isAd;

    private boolean isFirstResume = true;

    @Override
    protected int getViewResId() {
        return R.layout.activity_logo0;
    }

    @Override
    protected void init() {

        tvSkip.setVisibility(View.GONE);
        //透明状态栏
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }*/

        StatusBarUtils.translucentStatusBar(this);
        StatusBarUtil.setLightMode(this);

       /* SharedPreferences isFirstShared = getSharedPreferences("isFirst", MODE_PRIVATE);

        isFirst = isFirstShared.getBoolean("IsFirstOpen", true);
        if (isFirst && false) {
            img.setVisibility(View.GONE);
            tvSkip.setVisibility(View.GONE);

        } else {

            //startTime();
            startImg();

        }

        SharedPreferences.Editor editor = isFirstShared.edit();
        if (isFirst) {
            editor.putBoolean("IsFirstOpen", false);
        }
        editor.putBoolean("IsVerFirst", true);
        editor.commit();*/

        //intoNextPage();

        if (SharedPreferencesUtil.getAgreement(this)) {//已经同意过协议
            startTime();
            startImg();
        } else {

            AgreementPop agreementPop = new AgreementPop(this, this, this);
            agreementPop.showPopupWindow();
        }


    }

    @Override
    protected void loadData() {
        Map<String, String> map = new HashMap<>();

        map.put("code", "astgrsdagd");
        map.put("lang", "sdgadg");
        map.put("type", "2");

        OkHttpUtil.getJSON(this, Constant.URL.guides, this);
    }

    private void startImg() {
        img.setVisibility(View.VISIBLE);
        //  tvSkip.setVisibility(View.VISIBLE);

    }

    public void setData() {
        if (imgData == null) {
            imgData = new ArrayList<>();
        } else {
            imgData.clear();
        }


    }

    public void intoNextPage() {
//        SharedPreferencesUtil.saveToken(this, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhdWQiOiJlYjA1ZWE2ZiIsInN1YiI6IllNTjlKZDAzeGsiLCJtdCI6MCwiYXBwSWQiOiJ5NmkzZkxTREhyejhBaEphIiwiZXhwIjoxNTY1ODM2MTEwLCJpYXQiOjE1MzQyOTk4MTAsInZlcnNpb24iOjEwMDAwMn0.LvcFnDGBtFABSgKKIcyCQIrok6TE7dlDJMypqy7gxPpSQFe4b2wr2Gto1OUj_fVsbDpkbdqD0PJKBDapJ9DYgw");
        if (!isAd) {
            if (TextUtils.isEmpty(SharedPreferencesUtil.getToken(this))) {
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
            } else {
                RefreshtokenEntity.BodyBean roleInfo = SharedPreferencesUtil.getRoleInfo(this);
                if (TextUtils.isEmpty(roleInfo.getMember().getMobile())) {//没绑定
                    Intent intent = new Intent(this, BindPhone_V3Activity.class);
                    startActivity(intent);
                } else {
                    if (isHaveImg) {
                        Intent intent = new Intent(this, LogoActivity.class);
                        startActivity(intent);
                    } else {
                        //初始化推送
                        AppContext.getInstance().initPush();
                        Intent intent = new Intent(this, MainActivity.class);
                        startActivity(intent);
                    }
                }


            }

            finish();
        }


    }


    @OnClick(R.id.tv_skip)
    public void skip() {
        handler = null;
        countTime = 0;
        intoNextPage();
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }


    @Override
    public void onPageSelected(int position) {
        if (position == 4) {
            handler = new Handler();
            countTime = 3;//初始秒数
            runnable = new Runnable() {
                @Override
                public void run() {
                    if (handler != null && tvSkip != null) {
                        tvSkip.setText(countTime + "s  跳过>>");
                        if (countTime <= 0) {
                            intoNextPage();
                        } else {
                            countTime--;
                            handler.postDelayed(this, 1000);
                        }
                    }
                }
            };
            handler.post(runnable);
        } else {

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sdv: {
                if (position != -1) {
                    if (TextUtils.isEmpty(SharedPreferencesUtil.getToken(this))) {
                        return;
                    }
                    isAd = true;
                    if (guidesEntity.getBody() == null) {//jumpType": "integer,跳转类型，1 APP页面跳转，

                    }
                }
            }
            break;
            default:
                SharedPreferencesUtil.saveCheckAgreement(this,true);
                startTime();
                startImg();
        }
    }


    private void startTime() {
        try {
            handler = new Handler();
            countTime = 2;//初始秒数
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (handler != null && tvSkip != null) {
                        tvSkip.setText(countTime + "s  跳过>>");
                        if (countTime <= 0) {
                            intoNextPage();
                        } else {
                            countTime--;
                            handler.postDelayed(this, 1000);
                        }
                    }
                }
            });
        } catch (Exception e) {

        }

    }


    @Override
    public void onResponse(String url, String json) {
        LogeUtil.e("guides", json);
        // startTime();
        guidesEntity = gson.fromJson(json, GuidesEntity.class);
        if (guidesEntity.getBody() == null || guidesEntity.getBody().size() == 0) {
            isHaveImg = false;

        } else {
            isHaveImg = true;

        }
    }

    @Override
    public void onFailure(String url, String error) {
        LogeUtil.e("guides,error", error);
        //startTime();
    }

}
